package com.tcc.qa.test.util;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.xpath.operations.String;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.Reporter;

public class SelLogger extends BaseTest{

  private static Logger logger = null;

  private static SelLogger logg = null;

  public static SelLogger getLogger(Class<?> T) {
    if (logger == null) {
      Properties props = new Properties();

      try {
        InputStream is = new FileInputStream("src//cn//opentest//utils//log4j.properties");
        props.load(is);
      } catch (IOException e) {
        e.printStackTrace();
      }

      PropertyConfigurator.configure(props);
      logger = Logger.getLogger(T);
      logg = new SelLogger();
    }
    return logg;
  }

  // 重写logger方法
  public void log(String msg) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar ca = Calendar.getInstance();
    logger.info(msg);
    Reporter.log("Reporter:" + sdf.format(ca.getTime()) + "===>" + msg);
  }

  public void debug(String msg) {
    logger.debug(msg);
  }

  public void warn(String msg) {
    logger.warn(msg);
    Reporter.log("Reporter:" + msg);
  }

  public void warn(String msg, Exception e) {
    logger.warn(msg, e);
    Reporter.log("Reporter:" + msg);
  }

  public void pass(String msg) {
    logger.info(msg);
    Reporter.log("Reporter:" + msg);
  }

  public void error(String msg) {
    logger.error(msg);
    Reporter.log("Reporter:" + msg);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SS");
    Calendar ca = Calendar.getInstance();
    try{
      String filePath="logs//TestReport//Fail at";//"D://TestReport//Fail at ";
      File screenShotFile = ((TakesScreenshot)getWebDriver()).getScreenshotAs(OutputType.FILE);   
      FileUtils.copyFile(screenShotFile, new File(filePath+sdf.format(ca.getTime())+".png"));
    }catch(IOException e){
        e.printStackTrace();
    }
  }
}
